const mysql = require('mysql2/promise');

async function checkDatabase() {
    const connection = await mysql.createConnection({
        host: '43.143.253.188',
        user: 'nextjs',
        password: 'yEAiRE2jcxTyRBy5',
        database: 'nextjs'
    });

    try {
        console.log('🔍 检查数据库表状态...\n');

        // 检查character_skills表
        const [tables] = await connection.execute("SHOW TABLES LIKE 'character_skills'");
        if (tables.length > 0) {
            console.log('✅ character_skills表存在');

            // 检查表结构
            const [columns] = await connection.execute("DESCRIBE character_skills");
            console.log('📋 表结构:');
            columns.forEach(col => {
                console.log(`   ${col.Field} - ${col.Type} - ${col.Null} - ${col.Key} - ${col.Default} - ${col.Extra}`);
            });

            // 检查数据
            const [rows] = await connection.execute("SELECT COUNT(*) as count FROM character_skills");
            console.log(`📊 数据行数: ${rows[0].count}`);

            if (rows[0].count > 0) {
                const [sampleData] = await connection.execute("SELECT * FROM character_skills LIMIT 3");
                console.log('📝 示例数据:');
                sampleData.forEach(row => {
                    console.log(`   ID: ${row.id}, 角色ID: ${row.characterId}, 技能: ${row.skillName}`);
                });
            }
        } else {
            console.log('❌ character_skills表不存在');
        }

        // 检查characters表
        const [charTables] = await connection.execute("SHOW TABLES LIKE 'characters'");
        if (charTables.length > 0) {
            console.log('\n✅ characters表存在');
            const [charCount] = await connection.execute("SELECT COUNT(*) as count FROM characters");
            console.log(`📊 角色数量: ${charCount[0].count}`);

            if (charCount[0].count > 0) {
                const [sampleChars] = await connection.execute("SELECT id, name, roleType FROM characters LIMIT 5");
                console.log('👤 示例角色:');
                sampleChars.forEach(char => {
                    console.log(`   ID: ${char.id}, 名称: ${char.name}, 类型: ${char.roleType}`);
                });
            }
        } else {
            console.log('\n❌ characters表不存在');
        }

    } catch (error) {
        console.error('❌ 数据库检查失败:', error.message);
    } finally {
        await connection.end();
    }
}

checkDatabase().catch(console.error); 